import { jsticket, login } from "@/api/home/home";
import { siteTitle } from "@/settings";
// 数组对象去重
export function arrGoAgain(arr, type) {
  let typeId = type || 0;
  let newArr = [];
  let map = new Map();
  if (typeId == 0) {
    for (let item of arr) {
      if (!map.has(item.templateId)) {
        map.set(item.templateId, item);
      }
    }
  } else if (typeId == 1) {
    for (let item of arr) {
      if (!map.has(item.mealId)) {
        map.set(item.mealId, item);
      }
    }
  } else if (typeId == 2) {
    for (let item of arr) {
      if (!map.has(item.id)) {
        map.set(item.id, item);
      }
    }
  }
  newArr = [...map.values()];
  return newArr;
}

// 设置storage
export function getStorage(key) {
  return localStorage.getItem(key);
}
// 获取设置storage
export function setStorage(key, value) {
  return localStorage.setItem(key, value);
}
// 删除storage
export function removeStorage(key) {
  return localStorage.removeItem(key);
}

export function decimalPoint(val, type) {
  let dom = "";
  const splitPrice = (val.toString() || "0.00").split(".");
  const numb1 = splitPrice[0];
  var numb2 = "00";
  if (splitPrice.length > 1) {
    numb2 = splitPrice[1];
  }
  if ((type = 1)) {
    dom =
      `<span style="font-size:14px;line-height:14px">￥</span>` +
      `${numb1}.<span style="font-size:14px;line-height:14px;">${numb2}</span>`;
  } else {
    dom =
      `<span style="font-size:12px;line-height:12px">￥</span>` +
      `${numb1}.<span style="font-size:12px;line-height:12px;">${numb2}</span>`;
  }
  return dom;
}

// 微信/支付宝授权
export function authorizeLogin(type, code) {
  const token = getStorage(`${siteTitle}_token`);
  if (!token) {
    if (code) {
      return new Promise((resolve) => {
        login({ code, type }).then((v) => {
          const data = v.data;
          setStorage(`${siteTitle}_token`, data.token);
          resolve();
        });
      });
    } else {
      jsticket({ url: window.location.href, type }).then((v) => {
        const data = v.data;
        if (type == 1) {
          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
            data.appId +
            "&redirect_uri=" +
            encodeURIComponent(data.url) +
            "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        } else {
          window.location.href =
            "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=" +
            data.appId +
            "&redirect_uri=" +
            encodeURIComponent(data.url) +
            "&scope=auth_user";
        }
      });
    }
  }
}

// 保留N位小数方法
export function toFixedEvent(length, num) {
  let number = num;
  if (typeof num == "string") {
    number = parseFloat(num);
  }
  const decimal = String(num).indexOf(".") + 1;
  if (number != 0 && decimal > 0) {
    if (parseFloat(num.toFixed(length)) < 0) {
      return 0;
    } else {
      return parseFloat(num.toFixed(length));
    }
  } else {
    if (number < 0) {
      return 0;
    } else {
      return parseFloat(number);
    }
  }
}
